unit UThemeMgr;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, USingleGridBase, dxSkinsCore,
  dxSkinWhiteprint, System.ImageList, Vcl.ImgList, cxImageList, cxGraphics, dxBar,
  cxClasses, dxSkinBlue, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, cxStyles, cxEdit, cxDataControllerConditionalFormattingRulesManagerDialog,
  Data.DB, MemDS, DBAccess, Uni, cxInplaceContainer, cxVGrid, cxDBVGrid, UCommonConst,
  cxCustomData, cxFilter, cxData, cxDataStorage, cxNavigator, cxDBData,
  cxGridLevel, cxGridCustomView, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGrid, dxSkinOffice2016Colorful;

type
  TfrmThemeMgr = class(TfrmSingleGridBase)
    grdMainDBTableView1Column1: TcxGridDBColumn;
    grdMainDBTableView1Column2: TcxGridDBColumn;
    procedure FormCreate(Sender: TObject);
    procedure btnAddClick(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
    procedure grdMainDBTableView1CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure btnDeleteClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }

    procedure ShowAllData;
    procedure RefeshALLData(var message: TMessage); message WM_REFRESHDATA;
  end;

var
  frmThemeMgr: TfrmThemeMgr;

implementation

uses
  UCommonFun, UCommonVar, UThemeAdd, UCommonFunSqlite, UThemeEdit;

{$R *.dfm}

procedure TfrmThemeMgr.btnAddClick(Sender: TObject);
begin
  inherited;

  if Application.FindComponent('frmThemeAdd') = nil then
  begin
    frmThemeAdd := TfrmThemeAdd.Create(Application);
    try
      frmThemeAdd.ParentHandle := Handle;
      frmThemeAdd.Show;
    except
    end;
  end
  else
  try
    frmThemeAdd.BringToFront;
  except

  end;
end;

procedure TfrmThemeMgr.btnDeleteClick(Sender: TObject);
var
  sSql: string;
  iThemeID: Integer;
begin
  inherited;

  if (not qryMain.Active) or (qryMain.Eof) then
  begin
    MsgBoxDefault(RESNOTDELETEDATA);
    Abort;
  end;

  with qryMain do
  begin
    if Active then
      iThemeID := FieldByName('ThemeID').AsInteger
    else
      Exit;
  end;

  if MsgYesNoDefault(RESDBDELETECONFIRM) then
  begin
    sSql := 'delete from ThemeData where ThemeID=''' + IntToStr(iThemeID) + '''';
    if ExecSql(sSql) then
    begin
      SendMessage(Handle, WM_REFRESHDATA, 0, 0);
    end
    else
      MsgBoxDefault(RESDBDELETEFAIL);
  end;
end;

procedure TfrmThemeMgr.btnEditClick(Sender: TObject);
var
  iSelectThemeID: Integer;
begin
  inherited;

  if (not qryMain.Active) or (qryMain.Eof) then
  begin
    MsgBoxDefault(RESNOTEDITDATA);
    Abort;
  end;

  iSelectThemeID := qryMain.FieldByName('ThemeID').AsInteger;

  frmThemeEdit := TFrmThemeEdit.Create(Application);
  try
    frmThemeEdit.SelectID := iSelectThemeID;
    frmThemeEdit.ParentHandle := Handle;
    frmThemeEdit.ShowModal;
  finally
    frmThemeEdit.Free;
  end;
end;

procedure TfrmThemeMgr.FormCreate(Sender: TObject);
begin
  inherited;

  ShowAllData;
end;

procedure TfrmThemeMgr.grdMainDBTableView1CellDblClick(
  Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo;
  AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  inherited;

  btnEditClick(Self);
end;

procedure TFrmThemeMgr.ShowAllData;
var
  sSql: string;
begin
  sSql := 'select * from ThemeData order by ThemeID';

  with qryMain do
  begin
    Close;
    SQL.Text := sSql;
    Open;
  end;
end;

procedure TFrmThemeMgr.RefeshALLData(var message: TMessage);
begin
  ShowAllData;
end;

end.
